package sc;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

public class TopPanel extends BasePanel
{
	private static final long serialVersionUID = 1L;
	private static JPopupMenu popupMenu;
	private Image menuicon;
	private Image nameicon;
	private Image venticon;
	private Image chaticon;
	private Image rankicon;
	private static JMenuItem openForum;
	private static JMenuItem joinChat;
	private static JMenuItem joinVent;
	private static TopPanel instance;
	
	public TopPanel()
	{		
		ActionListener menuListener = new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				String cmd = e.getActionCommand();
				if (cmd == "Hide Member List")
				{
					Harlequin.getInstance().setVisible(false);
					Tray.showHideItem(false);
				}
				else if (cmd == "Open Forum")
				{
					OpenURL.openForum();
				}
				else if (cmd == "Join Chat")
				{
					OpenURL.joinChat();
				}
				else if (cmd == "Join Vent")
				{
					OpenURL.joinVent();
				}
				else if (cmd == "Configuration...")
				{
					Configuration.open();
				}
				else if (cmd == "Exit Harlequin")
					System.exit(0);
			}
		};

		JPopupMenu.setDefaultLightWeightPopupEnabled(false);
		popupMenu = new JPopupMenu("Menu");
		
		JMenuItem item = new JMenuItem("Hide Member List");
		item.addActionListener(menuListener);
		popupMenu.add(item);
		
		Icon icon = new ImageIcon(Images.getImage("/images/forum.png"));
		openForum = new JMenuItem("Open Forum", icon);
		openForum.addActionListener(menuListener);
		popupMenu.add(openForum);
		
		icon = new ImageIcon(Images.getImage("/images/inchat.png"));
		joinChat = new JMenuItem("Join Chat", icon);
		joinChat.addActionListener(menuListener);
		popupMenu.add(joinChat);
		
		icon = new ImageIcon(Images.getImage("/images/ventrilo.png"));
		joinVent = new JMenuItem("Join Vent", icon);
		joinVent.addActionListener(menuListener);
		popupMenu.add(joinVent);
		
		icon = new ImageIcon(Configuration.icon);
		item = new JMenuItem("Configuration...", icon);
		item.addActionListener(menuListener);
		popupMenu.add(item);
		
		popupMenu.addSeparator();
		item = new JMenuItem("Exit Harlequin");
		item.addActionListener(menuListener);
		popupMenu.add(item);
		
		popupMenu.addPopupMenuListener(new PopupMenuListener() {

			public void popupMenuCanceled(PopupMenuEvent e){}

			public void popupMenuWillBecomeInvisible(PopupMenuEvent e)
			{
				menuicon = Images.getImage("/images/menu0.png");
				repaint();
			}

			public void popupMenuWillBecomeVisible(PopupMenuEvent e)
			{
				menuicon = Images.getImage("/images/menu1.png");
				repaint();
			}
			
		});
		add(popupMenu);		
		
		menuicon = Images.getImage("/images/menu0.png");
		nameicon = Images.getImage("/images/sort1.png");
		venticon = Images.getImage("/images/sort0.png");
		chaticon = Images.getImage("/images/sort0.png");
		rankicon = Images.getImage("/images/sort0.png");
		
		setPreferredSize(new Dimension(100, 19));
		
		enableOpenForum(false);		
		enableJoinChat(false);
		enableJoinVent(false);
		
		instance = this;
	}
    
	public void paintComponent(Graphics g)
    {
		int w = getWidth();
		int pw = w;
		Member user = Member.userMember();
		if (user != null)
			pw = user.getWidth();
		
		// Tile the background image
		Image bg = Images.getImage("/images/panel.png");
		for (int x = 0; x < w; x += 37)
			g.drawImage(bg, x, 0, this);
		
		g.drawImage(menuicon, 0, 0, this);
		g.drawImage(nameicon, 30, 0, this);
		g.drawImage(venticon, pw - 60, 0, this);
		g.drawImage(chaticon, pw - 40, 0, this);
		g.drawImage(rankicon, pw - 20, 0, this);
	}

	public void mouseClicked(MouseEvent e)
	{
		int x = e.getX();
		if (x < 30)
			popupMenu.show(this, 6, 18);
		else if (x > 30 && x < BasePanel.width() - 60)
		{	
			PanelControl.sortByName();
			nameicon = Images.getImage("/images/sort1.png");
			venticon = Images.getImage("/images/sort0.png");
			chaticon = Images.getImage("/images/sort0.png");
			rankicon = Images.getImage("/images/sort0.png");
		}
		else if (x > BasePanel.width() - 60 && x < BasePanel.width() - 40)
		{	
			PanelControl.sortByVent();
			nameicon = Images.getImage("/images/sort0.png");
			venticon = Images.getImage("/images/sort1.png");
			chaticon = Images.getImage("/images/sort0.png");
			rankicon = Images.getImage("/images/sort0.png");
		}
		else if (x > BasePanel.width() - 40 && x < BasePanel.width() - 20)
		{	
			PanelControl.sortByChat();
			nameicon = Images.getImage("/images/sort0.png");
			venticon = Images.getImage("/images/sort0.png");
			chaticon = Images.getImage("/images/sort1.png");
			rankicon = Images.getImage("/images/sort0.png");
		}
		else if (x > BasePanel.width() - 20)
		{	
			PanelControl.sortByRank();
			nameicon = Images.getImage("/images/sort0.png");
			venticon = Images.getImage("/images/sort0.png");
			chaticon = Images.getImage("/images/sort0.png");
			rankicon = Images.getImage("/images/sort1.png");
		}
		repaint();
	}
	
	public static void enableOpenForum(boolean enable)
	{
		openForum.setEnabled(enable);	
	}
	
	public static void enableJoinChat(boolean enable)
	{
		joinChat.setEnabled(enable);	
	}
	
	public static void enableJoinVent(boolean enable)
	{
		joinVent.setEnabled(enable);	
	}
	
	public static void update()
	{
		TopPanel.instance.repaint();
	}
	
	public static void hidePopupMenu()
	{
		popupMenu.setVisible(false);
	}
}
